<?php
// for hacker
function clearUrl($Raw){
    $Raw = trim($Raw);
    $RemoveChars = array( "([^a-zA-Z0-9_.])" );
    $ReplaceWith = array("");
    return @preg_replace($RemoveChars, $ReplaceWith, $Raw);
} 
function info_lang_default($select){
    global $anyDB, $tb_fix;
    $sql = "select $select from ".$tb_fix."_lang where lang_default = 1";
    $q = $anyDB->query($sql);
    if($anyDB->numRows($q)>0){
        $r = $anyDB->getNext();
        return $r[$select];    
    }  
}
function find_lang_file($filename,$lang_code_default="vi") {
    $ext = '.php';
    $langDir = "_languages/"; 
    $search = $langDir.$filename.$ext;
    $found = false;
    if (is_file($search)) {
        // A matching file has been found
        $found = $search;
    }else{
        $found = $langDir.$lang_code_default.$ext;
    }
    return $found;
}
global $anyDB, $tb_fix;    
$preUrl = @$_SERVER['HTTP_REFERER'];    ;
$lang_default_define = info_lang_default('lang_code');
if(isset($_GET['lang'])){
    $lang = strtolower(clearUrl($_GET['lang']));
    $sql = "select count(lang_id) as colang from ".$tb_fix."_lang where lang_code = '$lang'";
    $r = $anyDB->getNext($anyDB->query($sql));        
    if($r['colang'] == 1){
        switch($lang){
            case "$lang": $_SESSION['mctlang'] = "$lang"; break;
            default: $_SESSION['mctlang'] = info_lang_default('lang_code'); break;
        }
        header("Location: $preUrl"); 
    }else{
        $_SESSION['mctlang'] = $lang_default_define;
    }
    
}else{
    $_SESSION['mctlang']= $lang_default_define;
}
$lang_file = find_lang_file($lang);  
include($lang_file);
?>